
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
def do(np,pd):
    ex2=pd.read_excel('./体测分数_女生.xls')
    ex1=pd.read_excel('./体测分数_男生.xls')
#饼图 男1000米(左)和男引体(右)饼图绘制百分比4.png
    fig, ((ax11, ax12)) = plt.subplots(1, 2)
    fig.set_figwidth(6)
    fig.set_figheight(3)
    ex1_tmp = ex1.copy()
    ex1_tmp['男1000米跑'] =pd.cut(ex1['男1000米跑'], bins=3)
    ex1_tmp['男引体'] =pd.cut(ex1['男引体'], bins=3)
    fig = plt.figure(figsize=(5, 5), dpi=150)
    # 偏移中⼼量，突出某⼀部分
    #男1000
    labels = ('good','middle','bad')
    ex1_tmp_run = ex1_tmp.groupby(ex1_tmp['男1000米跑'])['男1000米跑'].count()
    ax11.pie(x=ex1_tmp_run ,  # 数据
            # explode=explode,  # 偏移中⼼量
            labels=labels,  # 显示标签
            autopct='%0.1f%%',  # 显示百分⽐
            shadow=True)  # 阴影，3D效果
    # plt.tight_layout()
    # _ = plt.title(label='男1000米 ',
    #               fontsize=32, weight='bold',
    #               color='white', backgroundcolor='#c5b783')
    # 男引体

    ex1_tmp_run1 = ex1_tmp.groupby(ex1_tmp['男引体'])['男引体'].count()
    ax12.pie(x=ex1_tmp_run1,  # 数据
            # explode=explode,  # 偏移中⼼量
            labels=labels,  # 显示标签
            autopct='%0.1f%%',  # 显示百分⽐
            shadow=True)  # 阴影，3D效果
    # _ = plt.title(' 学历要求')
    # plt.tight_layout()
    # _ = plt.title(label='男引体 ',
    #             )
    # plt.savefig('./男1000米(左)和男引体(右)饼图绘制百分比4.png')
    plt.show()
    # print(ex1_tmp_run)
    # print(ex1_tmp_run['男1000米跑']['count'])
#、对女800米跑、女跳远进行直方图绘制统计各分数段人数，分成4份

def do1(np,pd):
    ex1=pd.read_excel('./体测分数_女生.xls')

#饼图 男1000米(左)和男引体(右)饼图绘制百分比4.png
    fig, ((ax11, ax12)) = plt.subplots(1, 2)
    fig.set_figwidth(6)
    fig.set_figheight(3)
    ex1_tmp = ex1.copy()
    # ex1_tmp['女800米跑'] =pd.cut(ex1['女800米跑'], bins=3)
    # ex1_tmp['男引体'] =pd.cut(ex1['男引体'], bins=3)
    fig = plt.figure(figsize=(5, 5), dpi=150)
    # 偏移中⼼量，突出某⼀部分
    #男1000
    labels = ('good','middle','bad')
    mu = 100  # 平均值
    sigma = 15  # 标准差
    x =ex1['女800米跑']
    df = pd.DataFrame({'女800米跑': x},
                      columns=['女800米跑'])
    df.plot.hist(alpha=0.5,density=1,bins=4)
    plt.savefig('./女800米跑直⽅图.png')
    plt.show()
def do12(np,pd):
    ex1=pd.read_excel('./体测分数_女生.xls')

#直方图 女跳远
    fig, ((ax11, ax12)) = plt.subplots(1, 2)
    fig.set_figwidth(6)
    fig.set_figheight(3)
    ex1_tmp = ex1.copy()
    # ex1_tmp['女800米跑'] =pd.cut(ex1['女800米跑'], bins=3)
    # ex1_tmp['男引体'] =pd.cut(ex1['男引体'], bins=3)
    fig = plt.figure(figsize=(5, 5), dpi=150)
    # 偏移中⼼量，突出某⼀部分
    #男1000
    labels = ('good','middle','bad')
    mu = 100  # 平均值
    sigma = 15  # 标准差
    x =ex1['女跳远']
    df = pd.DataFrame({'女800米跑': x},
                      columns=['女800米跑'])
    df.plot.hist(alpha=0.5,normed=1,bins=4)
    plt.savefig('./女跳远跑直⽅图.png')
    plt.show()
def do2(np,pd):
    ex2=pd.read_excel('../mlb/体测分数_女生.xls')
    ex1=pd.read_excel('../mlb/体测分数_男生.xls')
    labels = ['小','⼩猫','⼩⻦','肥胖']
#直方图 女跳远
    # fig, ((ax11, ax12)) = plt.subplots(1, 2)
    # fig.set_figwidth(6)
    # fig.set_figheight(3)
    ex1_tmp = ex1.copy()
    ex2_tmp = ex2.copy()
    count1 = ex1_tmp['BMI'].count()
    count2 = ex2_tmp['BMI'].count()
    # ex1_tmp['BMI_pate'] = np.select([(ex1_tmp['BMI']<=16.4),(16.5<ex1_tmp['BMI']<=23.2),(23.3<ex1_tmp['BMI']<=26.3),(23.3<ex1_tmp['BMI']<=26.3),(ex1_tmp['BMI']>=26.4)],[0,1,2,3])
    ex1_tmp['BMI_pate'] = np.select([(ex1_tmp['BMI']<=16.4),
                                     ((ex1_tmp['BMI']>16.4 )& (ex1_tmp['BMI']<=23.2)),
                                     ((ex1_tmp['BMI'] > 23.2) & (ex1_tmp['BMI'] <= 26.3)),
                                     ((ex1_tmp['BMI'] > 26.4)),
                                     ],[0,1,2,3])

    ex2_tmp['BMI_pate'] = np.select([(ex2_tmp['BMI'] <= 16.4),
                                     ((ex2_tmp['BMI'] > 16.4) & (ex2_tmp['BMI'] <= 22.7)),
                                     ((ex2_tmp['BMI'] > 22.7) & (ex2_tmp['BMI'] <= 25.2)),
                                     ((ex2_tmp['BMI'] > 25.2)),
                                     ], [0, 1, 2, 3])
    # print(ex1_tmp['BMI_pate'])
    # print(ex2_tmp['BMI_pate'])
    ex1_tmp_run = ex1_tmp.groupby(ex1_tmp['BMI_pate'])['BMI_pate'].count()
    ex2_tmp_run = ex2_tmp.groupby(ex2_tmp['BMI_pate'])['BMI_pate'].count()

    # labels1 = ('Low','Normal','Overweight','Obesity')
    labels1 = ('L','N','Ov','Ob')


    plt.pie(x=ex1_tmp_run,  # 数据
             # explode=explode,  # 偏移中⼼量
             radius=1,  # 半径
             pctdistance=0.85,  # 百分⽐位置
             labels=labels1,  # 显示标签
             autopct='%0.1f%%',  # 显示百分⽐
             shadow=True)  # 阴影，3D效果

    plt.pie(x=ex2_tmp_run,  # 数据
            # explode=explode,  # 偏移中⼼量
            radius=0.7,
            pctdistance=0.7,
            labels=labels1,  # 显示标签
            autopct='%0.1f%%',  # 显示百分⽐
            shadow=True)  # 阴影，3D效果
    # ex1_tmp['女800米跑'] =pd.cut(ex1['女800米跑'], bins=3)
    # ex1_tmp['男引体'] =pd.cut(ex1['男引体'], bins=3)
    # fig = plt.figure(figsize=(5, 5), dpi=150)
    # 偏移中⼼量，突出某⼀部分
    #男1000
    # labels = ('good','middle','bad')
    # plt.savefig('./女跳远跑直⽅图.png')
    wedgeprops = dict(linewidth=3, width=0.7, edgecolor='w')
    # 设置图例标题、位置，frameon控制是否显示图例边框，bbox_to_anchor控制图例显示在饼图的外⾯
    plt.legend(labels, loc='upper right', bbox_to_anchor=(0.75, 0, 0.4, 1), title=
    'BMI嵌套饼图')
    plt.savefig('./BMI嵌套饼图.png')

    plt.show()
if __name__=='__main__':
    #1、对男1000米跑、男引体进行等宽分箱操作，分成3份，并使用饼图绘制百分比
    do(np,pd)
    #、对女800米跑、女跳远进行直方图绘制统计各分数段人数，分成4份
    do1(np,pd)
    #对女跳远
    do12(np,pd)
    #使用嵌套饼图对比男女生体重指数BMI进行比例统计，分为正常、低体重、超重、肥胖
    do2(np,pd)